import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/home_business/widget/user_avatar.dart';
import 'package:xvideos/hj_page/home_business/widget/user_name_view.dart';
import 'package:xvideos/hj_page/mine_business/ai/ai_home_menu_page.dart';
import 'package:xvideos/hj_page/mine_business/collec_history_buy/col_his_buy_page.dart';
import 'package:xvideos/hj_page/mine_business/mine_following/mine_following_view.dart';
import 'package:xvideos/hj_page/mine_business/mine_video_cache/video_cache_page.dart';
import 'package:xvideos/hj_page/mine_business/mine_vip/mine_charge_coin_page.dart';
import 'package:xvideos/hj_page/mine_business/welfare/welfare_home_page.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'package:xvideos/hj_utils/holiday_theme.dart';
import 'package:xvideos/hj_utils/screen.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/global_store/store.dart';

import '../home_mine_logic.dart';
import '../mine_setting/mine_setting_page.dart';

class MineInfoWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<MineMainLogic>(
      builder: (controller) {
        return Container(
          margin: EdgeInsets.only(left: 16, top: 12),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                color: Colors.transparent,
                child: Row(
                  children: [
                    UserAvatar(
                      size: 60,
                      showBorder: false,
                      model: Publisher()
                        ..portrait = controller.userInfo?.portrait
                        ..vipLevel = (controller.userInfo?.isVip == true) ? 1 : 0,
                      onTap: (){
                        Get.toNamed(MineSettingPage.routeName);
                      },
                    ),
                    12.sizeBoxW,
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Consumer<GlobalStore>(
                          builder: (context, store, child) {
                            int level =  store.meInfo?.vipLevel ?? 0;
                            String vipTitle = '';
                            if (level == 1) vipTitle = 'VIP';
                            if (level == 2) vipTitle = 'SVIP';
                            if (level >= 3) vipTitle = '至尊SVIP';
                            return Row(
                              children: [
                                UserNameView(
                                  name: controller.userInfo?.name ?? '',
                                  fontSize: 20,
                                  fontWeight: FontWeight.w500,
                                  isVip: (controller.userInfo?.vipLevel ?? 0) > 0,
                                  nameColor: Colors.white,
                                ),
                                8.sizeBoxW,
                                Image.asset(
                                  store.isVIP() ? 'is_vip.png'.mineImgPath : 'is_no_vip.png'.mineImgPath,
                                  width: 24,
                                ),
                                2.sizeBoxW,
                                level > 0
                                    ? Container(
                                        alignment: Alignment.center,
                                        height: 20,
                                        padding: EdgeInsets.symmetric(horizontal: 6),
                                        decoration: BoxDecoration(
                                          color: Color(0xffF4CF9F),
                                          borderRadius: BorderRadius.circular(3),
                                        ),
                                        child: Text(
                                          vipTitle,
                                          style: textStyle(10, Color(0xff9D5D46), FontWeight.w400),
                                        ),
                                      )
                                    : Text(
                                        '您还不是会员',
                                        style: textStyle(10, Colors.white.withOpacity(.55), FontWeight.w400),
                                      )
                              ],
                            );
                          },
                        ),
                        5.sizeBoxH,
                        InkWell(
                          onTap: () {
                            Clipboard.setData(ClipboardData(text: (controller.userInfo?.uid ?? 0).toString()));
                            showToast('复制成功');
                          },
                          child: Text(
                            "ID ${controller.userInfo?.uid ?? ''} 复制",
                            style: TextStyle(
                              color: Colors.white.withOpacity(.45),
                              fontSize: 12,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              Spacer(),
              SizedBox(width: 4.w),
            ],
          ),
        );
      },
    );
  }
}

//充值/代理赚钱/ai科技入口
class MineTaskMenuWidget extends StatelessWidget {
  double get itemWidth => (screen.screenWidth - 8 * 2 - 20) / 3;
  double get scaleRate => itemWidth / 120;
  late final double height = itemWidth * 120 / 113;
  final double ratio = 115 / 70;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MineMainLogic>(
      builder: (controller) {
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 16),
          width: double.infinity,
          child: Row(
            children: [
              Expanded(
                child: AspectRatio(
                  aspectRatio: ratio,
                  child: InkWell(
                    onTap: () => Get.to(MineChargeCoinPage()),
                    child: _buildItem(
                      "当前余额 ${(globalStore.getWallet()?.amount ?? 0) + (globalStore.getWallet()?.income ?? 0)}",
                      HolidayTheme.theme.coin,
                      10,
                    ),
                  ),
                ),
              ),
              Expanded(
                child: AspectRatio(
                  aspectRatio: ratio,
                  child: InkWell(
                    onTap: () => Get.to(WelfareHomePage(), preventDuplicates: false, arguments: 0),
                    child: _buildItem(
                      "实现月入十万",
                      HolidayTheme.theme.agent,
                      15,
                    ),
                  ),
                ),
              ),
              Expanded(
                child: AspectRatio(
                  aspectRatio: ratio,
                  child: InkWell(
                    onTap: () => Get.to(
                      () => AiHomeMenuPage(),
                      preventDuplicates: false,
                    ),
                    child: _buildItem(
                      "玩出新花样",
                      HolidayTheme.theme.aiEnter,
                      15,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildItem(String title, String imagePath, double left) {
    return InkWell(
      child: Stack(
        children: [
          Positioned.fill(
            child: Image.asset(
              imagePath,
            ),
          ),
          Positioned(
            left: left,
            bottom: 15,
            child: Text(
              title,
              style: TextStyle(fontSize: 10, color: Colors.white, fontWeight: FontWeight.w400),
            ),
          )
        ],
      ),
    );
  }
}

class MineFunchtionGridView extends StatelessWidget {
  MineFunchtionGridView({super.key});
  final functionLabels = ['我的收藏', '历史记录', '我的下载', '我的关注'];
  final functionImgs = ['mine_post.png', 'mine_collect.png', 'mine_follow.png', 'mine_offical.png'];
  final functions = [
    () => Get.to(ColHisBuyPage(PageType.collect)),
    () => Get.to(ColHisBuyPage(PageType.history)),
    () => Get.to(() => VideoCachePage()),
    () => Get.to(() => MineFollowingPage()),
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(4),
        color: Colors.white.withOpacity(.05),
      ),
      child: GridView.builder(
        physics: NeverScrollableScrollPhysics(),
        padding: EdgeInsets.zero,
        shrinkWrap: true,
        itemCount: functionLabels.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4,
          crossAxisSpacing: 0,
          mainAxisSpacing: 0,
          childAspectRatio: 1,
        ),
        itemBuilder: (context, index) {
          String titleDesc = functionLabels[index];
          String imagePath = functionImgs[index];

          return InkWell(
            onTap: functions[index],
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(imagePath.mineImgPath, width: 24),
                5.sizeBoxH,
                Text(
                  titleDesc,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 12,
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
